<?php

/**
 * @author CHUN Dany <chundany@gmail.com>
 */
class Mod_User extends CI_Model
{
  
  public function getUsers($start, $num, $q)
  {
    $this->db->select('u.*, r.'.field('name').' AS '.field('roleName'))
        ->from(table('user').' u')
        ->join(table('role').' r', 'u.'.field('roleId').'=r.'.field('id'))
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('u.'.field('updatedAt'), 'DESC');
        
    // if($this->input->get('viewAll', '') == ''){
    //  $this->db->where('u.'.field('fullname').' REGEXP', $q)
    //      ->or_where('u.'.field('username').' REGEXP', $q)
    //      ->or_where('u.'.field('email').' REGEXP', $q)
    //      ->or_where('u.'.field('phone').' REGEXP', $q)
    //      ->or_where('r.'.field('name').' REGEXP', $q);
    // }
    
    $this->db->limit($num, $start);
    return $this->db->get();
  }
  
  public function getUserNum($q)
  {
    $objDb = $this->getAllUsers($q);
    return $objDb->num_rows();
  }
  
  public function getAllUsers($q)
  {
    $this->db->select('u.*, r.'.field('name').' AS '.field('roleName'))
        ->from(table('user').' u')
        ->join(table('role').' r', 'u.'.field('roleId').'=r.'.field('id'))
        ->where('u.'.field('deletedAt'), NULL)
        ->order_by('u.'.field('updatedAt'), 'DESC');
        
    // if($this->input->get('viewAll', '') == ''){
    //  $this->db->where('u.'.field('fullname').' REGEXP', $q)
    //      ->or_where('u.'.field('username').' REGEXP', $q)
    //      ->or_where('u.'.field('email').' REGEXP', $q)
    //      ->or_where('u.'.field('phone').' REGEXP', $q)
    //      ->or_where('r.'.field('name').' REGEXP', $q);
    // }
    
    return $this->db->get();
  }

  public function getUserById($Id)
  {
    $this->db->select('u.*, r.'.field('name').' AS '.field('roleName'))
        ->from(table('user').' u')
        ->join(table('role').' r', 'u.'.field('roleId').'=r.'.field('id'))
        ->where('u.'.field('deletedAt'), NULL)
        ->where('u.'.field('id'),$Id);
    $result = $this->db->get();
    
    return $result->row_array();
  }
  
  public function getRoleList()
  {
    $this->db->select(field('id').', '.field('name'));
    $this->db->from(table('role'));
    //$this->db->order_by(field('name'), 'ASC');
    $result = $this->db->get();
    
    $return = array('' => '');
    if($result->num_rows() > 0){
      foreach($result->result_array() as $row){
        $return[$row[field('id')]] = $row[field('name')];
      }
    }

    return $return;
  }
  
  public function insert($image = NULL)
  {
    $data = array(
      field('fname')    => $this->input->post('fname'), 
      field('lname')    => $this->input->post('lname'), 
      field('username') => $this->input->post('username'),
      field('password') => mkpwd($this->input->post('password')),
      field('email')    => $this->input->post('email'),
      field('phone')    => $this->input->post('phone'),
      field('roleId')   => $this->input->post('role'),
      field('image')    => $image,
      field('isDisable')=> 0,
      field('isVerify') => 1
    );
    
    $this->db->insert(table('user'), $data); 
    return $this->db->insert_id();
  }
  
  public function update($id, $image = NULL)
  {
    $password = $this->input->post('password', '');
    $disable  = $this->input->post('disable', false);

    $data = array(
      field('fname')    => $this->input->post('fname'),
      field('lname')    => $this->input->post('lname'),
      field('password') => mkpwd($password),
      field('email')    => $this->input->post('email'),
      field('phone')    => $this->input->post('phone'),
      field('roleId')   => $this->input->post('role'),
      field('image')    => $image,
      field('isDisable')=> ($disable == 1 ? 1 : 0)
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
    
    if(empty($password)) unset($data[field('password')]);
    if(empty($image)) unset($data[field('image')]);
      
    $this->db->where(field('id'),$id);
    $this->db->update(table('user'), $data); 
    return $this->db->affected_rows();
  }
  
  public function changePassword($id, $password)
  {
    $data = array(
      field('password') => mkpwd($this->input->post('password'))
    );
    $this->db->set(field('updatedAt'), 'NOW()', FALSE);
          
    $this->db->where(field('id'),$id);
    $this->db->update(table('user'), $data); 
    return $this->db->affected_rows();
  }
  
  public function delete($id)
  {
    $this->db->set(field('deletedAt'), 'NOW()', FALSE);
    $this->db->where(field('id'), $id);
    $this->db->update(table('user'));
    return $this->db->affected_rows();
  }
}